package com.sq.system.service.impl;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sq.system.entry.SampleItem;
import com.sq.system.entry.SampleRes;
import com.sq.system.entry.SampleResult;
import com.sq.system.mapper.SampleResultDao;
import com.sq.system.service.SampleItemService;
import com.sq.system.service.SampleResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



/**
 * (SampleResult)表服务实现类
 *
 * @author makejava
 * @since 2025-03-07 14:25:02
 */
@Service
public class SampleResultServiceImpl extends ServiceImpl<SampleResultDao, SampleResult> implements SampleResultService {
    @Autowired
    private SampleResultDao sampleResultDao;
    @Autowired
    private SampleItemService  sampleItemService;
    @Value("${suanfa}")
    private String suanfa;

    @Override
    public String start(Long sampleSetId) {
        List<SampleItem> sampleItemList=sampleItemService.list(new QueryWrapper<SampleItem>().eq("sample_set_id", sampleSetId));
        System.out.println(sampleItemList);
        List<HashMap<String,Object>> imageUrls=new ArrayList<>();
        sampleItemList.forEach(sampleItem->{
            if(sampleItem.getStatus()==0){
                HashMap<String,Object> map=new HashMap<>();
                map.put("image_url",sampleItem.getSampleItemPath());
                map.put("sampleItemId",sampleItem.getId());
                imageUrls.add(map);
                sampleItem.setStatus(1);
                this.sampleItemService.updateById(sampleItem);
            }
        });
        JSONArray jsonArray=new JSONArray(imageUrls);
        JSONObject dataJson=new JSONObject();
        dataJson.put("image_urls",jsonArray);
        String data=dataJson.toString();
        System.out.println(data);
        String res= "";
        try {
            res = HttpUtil.post(suanfa, data);
            System.out.println(res);
        }catch (Exception e){
         return "算法不在线";
        }
        JSONObject jsonObject = JSONUtil.parseObj(res);
        String json= jsonObject.get("data").toString();
        Gson gson = new Gson();
        Type listType = new TypeToken<List<SampleRes>>() {}.getType();
        List<SampleResult> sampleResultList=new ArrayList<>();
        // 解析JSON数组为Java列表
        List<SampleRes> sampleResList = gson.fromJson(json, listType);
        sampleResList.forEach(sampleRes -> {
            for (SampleRes.Fracture fracture : sampleRes.getFractures()) {
                SampleResult sampleResult=new SampleResult();
                sampleResult.setSampleItemId(sampleRes.getSampleItemId());
                sampleResult.setJiancenumber(Integer.valueOf(sampleRes.getFractures_sum()));
                sampleResult.setJiancename("裂缝");
                System.out.println("fracture:*****************{}"+fracture);
                sampleResult.setJiancedeep(fracture.getDepth_m());
                sampleResultList.add(sampleResult);
            }
        });
        System.out.println(sampleResultList);
        saveBatch(sampleResultList);
        return "检测完成";
    }
}

